package com.unicom.vw.modules.sys.controller;

import com.unicom.vw.common.annotation.SysLog;
import com.unicom.vw.common.utils.PageUtils;
import com.unicom.vw.common.utils.Query;
import com.unicom.vw.common.utils.R;
import com.unicom.vw.modules.sys.entity.DownloadAddressEntity;
import com.unicom.vw.modules.sys.service.DownloadAddressService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/downloadAddress")
public class DownloadAddressController extends AbstractController{
    @Autowired
    private DownloadAddressService downloadAddressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:downloadaddress:list")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<DownloadAddressEntity> downloadAddressList = downloadAddressService.queryList(query);
        int total = downloadAddressService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(downloadAddressList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:downloadaddress:info")
    public R info(@PathVariable("id") Integer id){
        DownloadAddressEntity downloadAddress = downloadAddressService.queryObject(id);

        return R.ok().put("downloadAddress", downloadAddress);
    }

    /**
     * 保存
     */
    @SysLog("保存下载地址")
    @RequestMapping("/save")
    @RequiresPermissions("sys:downloadaddress:save")
    public R save(@RequestBody DownloadAddressEntity downloadAddress){
        downloadAddressService.save(downloadAddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改下载地址")
    @RequestMapping("/update")
    @RequiresPermissions("sys:downloadaddress:update")
    public R update(@RequestBody DownloadAddressEntity downloadAddress){
        downloadAddressService.update(downloadAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除下载地址")
    @GetMapping(value = "/delete")
    @RequiresPermissions("sys:downloadaddress:delete")
    public R delete(String ids){
        String stringArray[] = ids.split(",");
        for (String strId:stringArray) {
            Integer id = Integer.parseInt(strId);
            DownloadAddressEntity downloadAddress = downloadAddressService.queryObject(id);
            downloadAddress.setSts("1");//0是正常，1是删除
            downloadAddressService.update(downloadAddress);
        }
        return R.ok();
    }

    /**
     *条件查询
     */
    @GetMapping(value = "/getAll")
    @RequiresPermissions("sys:downloadaddress:getAll")
    public R getAll(@RequestParam Map<String, Object> params){

        String value=String.valueOf(params.get("sidx"));

        if ("webSiteName".equals(value)){
            params.put("sidx","web_site_name");
        }

        Query query = new Query(params);

        List<DownloadAddressEntity> downloadAddressList = downloadAddressService.getAll(query);
        int total = downloadAddressService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(downloadAddressList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }
}
